const app = getApp()
Page({
    data: {
        video: {},
        play: true, // true : 播放 ｜ false : 暂停
        max: '',
        min: 0,
        value: 0,
        treatment_item_id:'',
        day:'',
        show:false,
    },
  
    onLoad: function (options) {
        let that = this
        that.setData({
          week: options.week?options.week:'',
          treatment_item_id:options.treatment_item_id?options.treatment_item_id:''
        })
        that.xInit(options.audio_id)
    },
    xInit:function(id){
      let that = this
      wx.cloud.callFunction({
        name: 'digital_therapy_extend',
        data: {
          action: "get_muse_info",
          _id:id
        }
      }).then(res => {
        that.setData({
          video:res.result[0]
        })
      })
    },
    xToAudio:function(){
      let that = this
      let params = {
        week:that.data.week,
        treatment_item_id:that.data.treatment_item_id,
      }
      wx.navigateTo({
        url: '/subtool/pages/rudio/detail/index?treatment_item_id=' + params.treatment_item_id +'&audio_id=' + that.data.video._id +'&week=' +params.week
      })
    },
    bindloadedmetadata:function(e){
      this.setData({
        value:e.detail.duration
      })
    },
    bindpause(e){
      console.log('暂停播放--'+JSON.stringify(e))
    },
    onShow: function () {
        let that = this 
    },
    onHide: function () {
        let that = this
        
    },
    xTostop:function(){
        this.bindended()
    },
    back: function () {
        wx.navigateBack({
            delta: 1,
            fail: () => {
                wx.switchTab({
                    url: '/pages/navigation/index/index',
                })
            }
        })
    },
    
		xAudioSetTime: function (e) {
        let that = this
        let time = e.detail.value
        that.setData({
            value: time
        })
    },

    bindended: function () {
        let that = this
				let _dataObj = {
          week: that.data.week,
          job_type: 3,
          treatment_item_id:that.data.treatment_item_id,
          day:that.data.day,
          meditation_exercises_return: {
              meditation_id: that.data.video._id,
              meditation_name: that.data.video.name,
              meditation_duration: that.data.value,
              is_complete: true
          }
      }
      wx.cloud.callFunction({
          name: 'digital_therapy',
          data: {
              action: "add_job_record",
              dataObj: _dataObj
          },
          success: () => {
               that.setData({
                 show:true
               })
          }
      })
    }
})